import 'dart:ui';

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
      ),
      home: RRectDemo(),
    );
  }
}

class RRectDemo extends StatefulWidget {
  @override
  _RRectDemoState createState() => _RRectDemoState();
}

class _RRectDemoState extends State<RRectDemo> {
  Offset? _clickOffset;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('绘制专题')),
      body: Center(
        child: Container(
          width: 500,
          height: 500,
          color: Colors.blue.shade100,
          child: CustomPaint(
            size: Size(500, 500),
            //画布
            //绘制工作
            painter: RectPainter(),
          ),
        ),
      ),
    );
  }
}

class RectPainter extends CustomPainter {
  Paint _paint = Paint()
    ..color = Colors.red
    ..style = PaintingStyle.fill
    ..strokeCap = StrokeCap.round
    ..strokeWidth = 3;
  @override
  void paint(Canvas canvas, Size size) {
    //根据两个对角点来创建矩形
    Rect rect4 = Rect.fromPoints(Offset(100, 100), Offset(400, 400));

    _paint.color = Colors.red;
    _paint.strokeWidth = 3;
    //绘制
    // canvas.drawRect(rect4, _paint);

    _paint.color = Colors.yellow.withValues(alpha: 0.8);
    //根据矩形来创建圆角矩形  同时设置四个圆角
    // canvas.drawRRect(RRect.fromRectXY(rect4, 150, 150), _paint);

    //根据矩形来创建圆角矩形  可以分别设置圆角
    // canvas.drawRRect(
    //   RRect.fromRectAndCorners(
    //     rect4,
    //     topLeft: Radius.circular(200),
    //     bottomRight: Radius.circular(200),
    //   ),
    //   _paint,
    // );

    // canvas.drawRRect(
    //   RRect.fromLTRBR(100, 100, 300, 300, Radius.circular(40)),
    //   _paint,
    // );

    // canvas.drawRRect(RRect.fromLTRBXY(100, 100, 300, 300, 40, 40), _paint);

    canvas.drawRRect(
      RRect.fromLTRBAndCorners(
        100,
        100,
        300,
        300,
        topLeft: Radius.circular(40),
      ),
      _paint,
    );

    _paint.color = Colors.blue;
    _paint.strokeWidth = 10;
    canvas.drawPoints(PointMode.points, [Offset(100, 100)], _paint);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return true;
  }
}
